package org.itboys.pay.tools;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

import com.google.common.collect.Lists;
/**
 * 数据操作工具类(不同类型的数字之间的加减乘除)
 * @author tangjc
 *
 */
public class NumberCalculateUtils {
	private static List<String>numSet=Lists.newArrayList("String","Integer","Double","BigDecimal","Long","Decimal");
	/**
	 * 将输入的任意类型的数字转换为Bigdecimal
	 * @param o
	 * @return
	 */
	private static BigDecimal vali(Object o){
		if (null==o) {
			throw new RuntimeException("the number is null !");
		}
		if (!numSet.contains(o.getClass().getSimpleName())) {
			throw new RuntimeException("unsupport format !");
		}
		if ("BigDecimal".equals(o.getClass().getSimpleName())) {
			return (BigDecimal)o;
		}else {
			return new BigDecimal(o.toString());
		}
	}
	/**
	 * 
	 * @param c
	 * @param o
	 * @return
	 */
	private static Object Format(Class c, BigDecimal o) {
		try {
			switch (c.getSimpleName()) {
			case "BigDecimal":
				return o;
			case "Integer":
				return o.intValue();
			case "Long":
				return o.longValue();
			case "String":
				return o.toString();
			case "Double":
				return o.doubleValue();
			default:
				return  c.getConstructor(new Class[]{String.class}).newInstance(o.toString());
			}
			
		} catch (Exception e) {
			Throwable targetEx = ((InvocationTargetException) e)
                    .getTargetException();
            if (targetEx != null)
            {
               System.out.println( targetEx.getMessage());
            }
			return null;
		}
	} 
	/**
	 * 不同类型之间的数据相加
	 * @param d
	 * @return
	 */
	public static Object add(Object... d){
		BigDecimal result=BigDecimal.ZERO;
		Class c=d[0].getClass();
		for (Object d1 : d) {
			result=result.add(vali(d1));
		}
		return Format(c, result);
	}
	/**
	 * 不同类型之间的数据相减
	 * @param d
	 * @return
	 */
	public static Object subtract(Object... d){
		BigDecimal result=BigDecimal.ZERO;
		Class c=d[0].getClass();
		result=result.add(vali(d[0]));
		for (int i = 1; i < d.length; i++) {
			result=result.subtract(vali(d[i]));
		}
		return Format(c, result);
	}
	/**
	 * 不同类型之间的数据相乘
	 * @param d
	 * @return
	 */
	public static Object multiply(Object... d){
		BigDecimal result=vali(d[0]);
		Class c=d[0].getClass();
		for (int i = 1; i < d.length; i++) {
			result=result.multiply(vali(d[i]));
		}
		return Format(c, result);
	}
	/**
	 * 判断value对象是否为null  如果是返回给定的默认值  不是则返回对象的字符串
	 * @param value
	 * @param def
	 * @return
	 */
	public static String valueOrDef(Object value,String def){
		return null==value?def:value.toString();
	}
	/**
	 * 不同类型之间的数据相除
	 * @param d
	 * @return
	 */
	public static Object divid(Object... d){
		BigDecimal result=BigDecimal.ZERO;
		Class c=d[0].getClass();
		result=result.add(vali(d[0]));
		for (int i = 1; i < d.length; i++) {
			BigDecimal d2=vali(d[i]);
			if (d2.compareTo(BigDecimal.ZERO)==0) {
				return Format(c, BigDecimal.ZERO);
			}
			result=result.divide(d2, 10, BigDecimal.ROUND_HALF_UP);
		}
		return Format(c, result);
	}
}
